/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.supplement;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * 填充支付补单链路参数,查询第三方支付中订单
 *
 * @author huahua
 * 2024/8/29
 */
@LiteflowComponent(value = FlowPaySupplementNodeConst.QUERY_THIRD_PAYING_ORDER_NODE)
@AllArgsConstructor
public class QueryThirdPayingOrderNode extends NodeComponent {
    private final IOmsOrderService iOmsOrderService;
    private final LFLog log = LFLoggerManager.getLogger(FlowExecutor.class);

    @Override
    public void process() throws Exception {
        PaySupplementContext contextBean = getContextBean(PaySupplementContext.class);
        //查询支付中-第三方订单
        Page<OmsOrderVo> page = iOmsOrderService.queryThirdPayingOrderPageList(contextBean.getPageQuery());
        if (page != null && ObjectUtil.isNotEmpty(page.getRecords())) {
            List<OmsOrderVo> wxPayingOrderList = new ArrayList<>();
            List<OmsOrderVo> aliPayingOrderList = new ArrayList<>();
            for (OmsOrderVo omsOrderVo : page.getRecords()) {
                if (OrderEnums.PayType.WX_PAY.contains(omsOrderVo.getPayType())) {
                    wxPayingOrderList.add(omsOrderVo);
                } else if (OrderEnums.PayType.ALI_PAY.contains(omsOrderVo.getPayType())) {
                    aliPayingOrderList.add(omsOrderVo);
                }
            }
            contextBean.setWxPayingOrderList(wxPayingOrderList);
            contextBean.setAliPayingOrderList(aliPayingOrderList);
        }
        PaySupplementContext.SupplementResult result = new PaySupplementContext.SupplementResult();
        result.setSize(page.getSize());
        result.setCurrent(page.getCurrent());
        result.setTotal(page.getTotal());
        result.setHasNext(page.hasNext());
        contextBean.setSupplementResult(result);
    }
}
